package com.tools_s7.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.widget.RemoteViews;

import com.tools.s7.utils.DateUtils;
import com.tools_s7.R;
import com.tools_s7.activity.MainActivity;

public class HintService extends Service {

//    private Notification.Builder mNotification;
//    private NotificationManager mNotificationManager;
//    private PendingIntent mResultIntent;

    private NotificationManager mNotificationManager;
    private RemoteViews customView;

    @Override
    public IBinder onBind(Intent intent) {
        return new DeviceIBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    /**
     * 通信数据传输
     * 实现方法
     */
    public class DeviceIBinder extends Binder {
        /**
         * 发送通知
         */
        public void getBinderData() {
            mNotificationManager.notify(Context.BIND_AUTO_CREATE, getNotification());
        }

        /**
         * 发送通知
         */
        public void getBinderData2() {
            customView.setTextViewText(R.id.hint_title, "系统提醒");
            customView.setTextViewText(R.id.hint_hint, "这是一条测试消息");
            mNotificationManager.notify(Context.BIND_AUTO_CREATE, getRemoteNotification());
        }
    }

    /**
     * 初始化通知栏
     */
    private Notification getNotification() {
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, new Intent[]{intent}, 0);
        Notification.Builder builder = new Notification.Builder(this)
                // 设置小图标
                .setSmallIcon(R.mipmap.ic_launcher)
                // 设置小图标旁的文本信息
                .setContentInfo("1")
                // 设置状态栏文本标题
                .setTicker("你的系统有更新")
                // 设置标题
                .setContentTitle("系统更新" + DateUtils.getTimeAccurate())
                // 设置内容
                .setContentText("发现系统更新，点击查看详情")
                // 设置ContentIntent
                .setContentIntent(pendingIntent)
                // 设置Notification提示铃声为系统默认铃声
                .setSound(RingtoneManager.getActualDefaultRingtoneUri(
                        getBaseContext(), RingtoneManager.TYPE_NOTIFICATION))
                // 点击Notification的时候自动移除
                .setAutoCancel(true);

        //设置Notification的ChannelID,否则不能正常显示
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("1");
        }
        Notification mNotification = builder.build();
        /*if (Build.VERSION.SDK_INT < 14) {
            mNotification.contentView = customView;
        }*/
        return mNotification;
    }

    /**
     * 初始化通知栏
     */
    private Notification getRemoteNotification() {
        if (customView == null) {
            customView = new RemoteViews(getPackageName(), R.layout.service_hint);
        }
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivities(this,
                0, new Intent[]{intent}, 0);
        Notification.Builder builder = new Notification.Builder(this)
                // 设置小图标
                .setSmallIcon(R.mipmap.ic_launcher)
                // 设置状态栏文本标题
                .setTicker("你的系统有更新")
                //设置自定义布局
                .setContent(customView)
                // 设置ContentIntent
                .setContentIntent(pendingIntent)
                // 设置Notification提示铃声为系统默认铃声
                .setSound(RingtoneManager.getActualDefaultRingtoneUri(
                        getBaseContext(), RingtoneManager.TYPE_NOTIFICATION))
                // 点击Notification的时候自动移除
                .setAutoCancel(true);

        //设置Notification的ChannelID,否则不能正常显示
//        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            builder.setChannelId("");
//        }
        Notification mNotification = builder.build();
//        if (Build.VERSION.SDK_INT < 14) {
//            mNotification.contentView = customView;
//        }
        return mNotification;
    }
}
